package com.example.imagethumbnail.controller;

import com.example.imagethumbnail.service.ThumbnailAwtService;
import com.example.imagethumbnail.service.ThumbnailService;
import com.example.imagethumbnail.service.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("thumbnail")
@Slf4j
public class ThumbnailController {
 
    @Autowired
    private UploadService uploadService;

    @Autowired
    private ThumbnailService thumbnailService;

    @Autowired
    private ThumbnailAwtService thumbnailAwtService;

    @PostMapping(value = "/1")
    public String thumbnail(@RequestParam(value = "image",required = true) MultipartFile file
                            , HttpServletRequest request
                            , Model model)throws IOException {
        String uploadPath = "/images";
        String mypath =System.getProperty("user.dir");
        log.info("mypath={}",mypath);

        HttpSession session = request.getSession();
        String realUploadPath = session.getServletContext().getRealPath(uploadPath);

        log.info("file={}",file);
        log.info("uploadPath={}",uploadPath);
        log.info("realUploadPath={}",realUploadPath);

        if(file==null){
            log.info("file={}",file);
        }

        String imageUrl = uploadService.uploadImage(file,uploadPath,realUploadPath);
 
        String thumImageUrl = this.thumbnailAwtService.thumbnail(file,uploadPath,realUploadPath);

        log.info("imageUrl={}",imageUrl);
        log.info("thumImageUrl={}",thumImageUrl);
        model.addAttribute("imageUrl",imageUrl);
        model.addAttribute("thumbImageUrl",thumImageUrl);


        return "/hello/thumbnail";

    }

    @PostMapping(value = "/2")
    public String thumbnail2(@RequestParam(value = "image",required = true) MultipartFile file
            , HttpServletRequest request
            , Model model)throws IOException {
        String uploadPath = "/images";
        String mypath =System.getProperty("user.dir");
        log.info("mypath={}",mypath);

        HttpSession session = request.getSession();
        String realUploadPath = session.getServletContext().getRealPath(uploadPath);

        log.info("file={}",file);
        log.info("uploadPath={}",uploadPath);
        log.info("realUploadPath={}",realUploadPath);

        if(file==null){
            log.info("file={}",file);
        }

        String imageUrl = uploadService.uploadImage(file,uploadPath,realUploadPath);
        String thumImageUrl = this.thumbnailService.thumbnail(file,uploadPath,realUploadPath);


        log.info("imageUrl={}",imageUrl);
        log.info("thumImageUrl={}",thumImageUrl);
        model.addAttribute("imageUrl",imageUrl);
        model.addAttribute("thumbImageUrl",thumImageUrl);


        return "/hello/thumbnail";

    }
}
